/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.dundd.daoimpl;

import cz.muni.fi.pa165.dundd.entity.HeroRole;
import cz.muni.fi.pa165.dundd.dao.HeroRoleDAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author xkonval
 */
@Repository
public class HeroRoleDAOImpl implements HeroRoleDAO {

    @PersistenceContext
    private EntityManager em;

    @Override
    public void createRole(HeroRole role) {
        em.persist(role);
    }

    @Override
    public void updateRole(HeroRole role) {
        em.merge(role); 
    }

    @Override
    public void deleteRole(HeroRole role) {
        em.remove(em.contains(role) ? role : em.merge(role));
    }

    @Override
    public HeroRole retrieveRoleById(long id) {
        HeroRole found = em.find(HeroRole.class, id);
        return found;
    }

    @Override
    public List<HeroRole> retrieveAllRoles() {
        Query query = em.createQuery(
                "select r from HeroRole r");
        return query.getResultList();
    }
}
